home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10881 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: apoll.informatik.uni-bonn.de!zeus!schregle
  2. From: schregle@zeus.informatik.uni-bonn.de (Roland Schregle)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual Base Class
  5. Date: 11 Mar 1996 16:27:14 GMT
  6. Organization: Universit"at Bonn, Informatik Abt. II, R"omerstr. 164, 53117 Bonn
  7. Message-ID: <4i1k92$3n8@apoll.informatik.uni-bonn.de>
  8. References: <313F98D0.102E@ucla.edu>
  9. NNTP-Posting-Host: zeus.informatik.uni-bonn.de
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Dennis Rahaman (dennisr@ucla.edu) wrote:
  13. : This is what I want to do:
  14.  
  15. :               a
  16. :             /   \
  17. :            b     c
  18. :             \   /
  19. :               d
  20.  
  21.  
  22. : class a
  23. :   {
  24. :   //...
  25. :   };
  26.  
  27. : class b : public virtual a
  28. :   {
  29. :   //...
  30. :   };
  31.  
  32. : class c : public virtual b
  33. :   {
  34. :   //...
  35. :   };
  36.  
  37. : class d : public b, public c
  38. :   {
  39. :   //...
  40. :   };
  41.  
  42.  
  43. : void f ()
  44. :   {
  45. :   a a1;
  46. :   d* pd = (d*) &a1;  // error: can't cast virtual base to derived
  47. :   }
  48.  
  49. : ///////////////////////////////////////////////////////////
  50. : Can someone explain why I can't cast a virtual base class to a derived 
  51. : class?
  52.  
  53. : What should I do instead?
  54.  
  55. : Any help would be appreciated.
  56.  
  57.  
  58.  
  59. I have the same problem! Somebody suggested the following (in terms of the
  60. above egg sample):
  61.  
  62. void f()
  63. {
  64.    a a1;
  65.    d* pd = (d*)(void*)&a1;
  66. }
  67.  
  68. I haven't tried this yet. C'mon you C++ cracks out there!!! :)
  69.  
  70.  
  71. GanjaTron
  72.